package com.nova.scenic.projectlibs.util.view;

import android.annotation.SuppressLint;
import android.os.SystemClock;
import android.view.InputDevice;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;

import com.nova.scenic.projectlibs.util.Applications;
import com.nova.scenic.projectlibs.util.debug.MyLog;

@SuppressLint("NewApi")
public class KeyUtil {

	public static boolean isOk(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_ENTER;
	}

	public static boolean isDown(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_DPAD_DOWN;
	}

	public static boolean isBack(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_BACK || keyCode == 111;
	}

	public static boolean isUp(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_DPAD_UP;
	}

	public static boolean isEnter(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_ENTER;
	}

	public static boolean isLeft(int keyCode) {
		return KeyEvent.KEYCODE_DPAD_LEFT == keyCode;
	}

	public static boolean isRight(int keyCode) {
		return KeyEvent.KEYCODE_DPAD_RIGHT == keyCode;
	}

	private static void inputModeChangeToKeyBoard(int keyCode) {
		if (Applications.isInTouchMode()) {
			if (KeyUtil.isDirectionKey(keyCode)) {
				// 除了方向按键，我们认为都是输入TouchMode
				Applications.setTouchMode(false);
				Applications.notifyInputModeChange(false);
				MyLog.p("input mode change to keyBoad");
			}
		}
	}

 

	public static boolean isDirectionKey(int keyCode) {
		return isUp(keyCode) || isDown(keyCode) || isLeft(keyCode) || isRight(keyCode);
	}

	public static boolean isUpOrDown(int keyCode) {
		// TODO Auto-generated method stub
		return isUp(keyCode) || isDown(keyCode);
	}

	public static boolean isLeftOrRight(int keyCode) {
		// TODO Auto-generated method stub
		return isLeft(keyCode) || isRight(keyCode);
	}

	public static void sendKeyEvent(final int keyCode) {
		new Thread(new Runnable() {
		
			@Override
			public void run() {
				long now = SystemClock.uptimeMillis();
				injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD));
				injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD));

			}
		}).start();
	}

	public static void injectKeyEvent(KeyEvent event) {
//		InputManager.getInstance().injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
	}


	public static boolean isdot(int keyCode) {
		return keyCode == 56;
	}

	public static boolean isActionUp(KeyEvent event) {
		// TODO Auto-generated method stub
		return event.getAction() == KeyEvent.ACTION_UP;
	}
	public static boolean isActionDown(KeyEvent event) {
		// TODO Auto-generated method stub
		return event.getAction() == KeyEvent.ACTION_DOWN;
	}

	@SuppressLint("NewApi")
	public static boolean isTouchBoardKey(int keyCode) {
//		if (keyCode == KeyEvent.KEYCODE_RC_OFN_LEFT || keyCode == KeyEvent.KEYCODE_RC_OFN_RIGHT || keyCode == KeyEvent.KEYCODE_RC_OFN_UP || keyCode == KeyEvent.KEYCODE_RC_OFN_DOWN) {
//			return true;
//		}
		return false;
	}

//	public static boolean isTouchBoardLeft(int keyCode) {
//		return (keyCode == KeyEvent.KEYCODE_RC_OFN_LEFT);
//	}
//
//	public static boolean isTouchBoardRight(int keyCode) {
//		return (keyCode == KeyEvent.KEYCODE_RC_OFN_RIGHT);
//	}
//
//	public static boolean isTouchBoardUp(int keyCode) {
//		return (keyCode == KeyEvent.KEYCODE_RC_OFN_UP);
//	}
//
//	public static boolean isTouchBoardDown(int keyCode) {
//		return (keyCode == KeyEvent.KEYCODE_RC_OFN_DOWN);
//	}
//	
	public static void hideIME(){
//		InputMethodManager manager = (InputMethodManager) sContext.get().getSystemService(Context.INPUT_METHOD_SERVICE);
//		manager.toggleSoftInput(1, 0);
	
	}

	public static void showIME() {
//		InputMethodManager manager = (InputMethodManager) sContext.get().getSystemService(Context.INPUT_METHOD_SERVICE);
//		manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
	}
	
	
	public static boolean isDelete(int keyCode) {
		return keyCode == KeyEvent.KEYCODE_DEL;
	}

	public static boolean isVoice(int keyCode) {
		return keyCode == 265;
	}

	public static boolean isTouchBoardUp(int keyCode) {
		return false;
	}

	public static boolean isTouchBoardDown(int keyCode) {
		return false;
	}

	

}
